package com.ubaer.zsycz.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.lsw.util.DensityUtil;
import com.lsw.util.StringUtil;
import com.lsw.view.ButtonTouch;
import com.ubaer.zsycz.R;

/**
 * Created by Administrator on 2018/9/25 0025.
 */

public class SuccessDialog {
    public Dialog alertDialog;
    protected Context context;
    View view;
    //    ButtonTouch btnOk, btnCancel;
    ImageView imageview;
    TextView pub_tip, tv_fail;


    public SuccessDialog(Context context) {
        this.context = context;
        init();
    }

    @SuppressLint("InflateParams")
    @SuppressWarnings("deprecation")
    private void init() {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.dialog_success, null);
        view.setMinimumWidth(DensityUtil.dip2px(context, 300));
//        btnOk = view.findViewById(R.id.btnOk);
        pub_tip = view.findViewById(R.id.pub_tip);
        imageview = view.findViewById(R.id.imageview);
        tv_fail = view.findViewById(R.id.tv_fail);
        alertDialog = new Dialog(context, R.style.dialogStyle);
        alertDialog.setContentView(view);
//        alertDialog.setCancelable(false);
        Window dialogWindow = alertDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
    }

    /**
     * @param type 1成功 2失败
     */
    public void show(int type, String error) {
        if (type == 1) {
            pub_tip.setText("提交成功！");
            imageview.setImageResource(R.mipmap.chenggong);
            tv_fail.setVisibility(View.GONE);
            pub_tip.setTextColor(context.getResources().getColor(R.color.color_115B8D));
        } else {
            pub_tip.setText("提交失败！");
            pub_tip.setTextColor(context.getResources().getColor(R.color.color_C73922));
            imageview.setImageResource(R.mipmap.shibai);
            tv_fail.setVisibility(View.VISIBLE);
            if (!StringUtil.isNullOrEmpty(error))
                tv_fail.setText(error);
        }
        if (!alertDialog.isShowing()) {
            alertDialog.show();
        }
    }

    public void dismiss() {
        if (alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
    }
}
